package com.ls.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ls.dto.EmployeeDto;
import com.ls.mapper.EmployeeMapper;
import com.ls.service.EmployeeService;
import com.ls.utils.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加员工
     * @param employeeDto
     * @return
     */
    @Override
    public Boolean add(EmployeeDto employeeDto) {
        try {
            System.out.println("=========>add:"+employeeDto);
            Integer add = employeeMapper.add(employeeDto);
            System.out.println(add);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 修改员工信息
     * @param employeeDto
     * @return
     */
    @Override
    public Boolean update(EmployeeDto employeeDto) {
        System.out.println("update:"+employeeDto);
        try {
            employeeMapper.update(employeeDto);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除员工信息
     * @param employeeDto
     * @return
     */
    @Override
    public Boolean delete(EmployeeDto employeeDto) {
        try {
            employeeMapper.delete(employeeDto);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改状态信息
     * @param employeeDto
     * @return
     */
    @Override
    public Boolean updStatus(EmployeeDto employeeDto) {

        try {
            employeeMapper.updStatus(employeeDto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 员工是否存在
     * @param employeeDto
     * @return
     */
    @Override
    public Boolean isRepeat(EmployeeDto employeeDto) {

        try {
            employeeMapper.isRepeat(employeeDto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 查看员工信息
     * @param employeeDto
     * @return
     */
    @Override
    public Status select(EmployeeDto employeeDto) {
        try {
            return new Status().setStatus(true).setMsg(employeeMapper.select(employeeDto));
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("操作失败~");
        }
    }

    /**
     * 获取员工信息(带筛选状态)
     * @param employeeDto
     * @return
     */
    @Override
    public Status getEmployees(EmployeeDto employeeDto) {
        PageHelper.startPage(employeeDto.getPage(),employeeDto.getPageSize());
        PageInfo employees = new PageInfo(employeeMapper.getEmployees(employeeDto));
        try {
            return new Status().setStatus(true).setMsg(employees);
        } catch (Exception e) {
            return new Status().setStatus(false).setMsg("操作失败~");
        }
    }

    /**
     * 添加员工
     * @param employeeDto
     * @return
     */
    @Override
    public Status addResult(EmployeeDto employeeDto) {
        System.out.println("====>addResult:"+employeeDto);
        if (add(employeeDto)){
            return new Status().setStatus(true).setMsg("添加成功~");
        }

        return new Status().setStatus(false).setMsg("添加失败~");
    }

    /**
     * 修改员工信息
     * @param employeeDto
     * @return
     */
    @Override
    public Status updateResult(EmployeeDto employeeDto) {

        if (update(employeeDto)){
            return new Status().setStatus(true).setMsg("修改成功~");
        }
        return new Status().setStatus(false).setMsg("修改失败~");
    }

    /**
     * 删除员工信息
     * @param employeeDto
     * @return
     */
    @Override
    public Status deleteResult(EmployeeDto employeeDto) {

        if (delete(employeeDto)){
            return new Status().setStatus(true).setMsg("删除成功~");
        }
        return new Status().setStatus(false).setMsg("删除失败~");
    }

    /**
     * 修改状态信息
     * @param employeeDto
     * @return
     */
    @Override
    public Status updStatusResult(EmployeeDto employeeDto) {

        if (updStatus(employeeDto)){
            return new Status().setStatus(true).setMsg("修改成功~");
        }
        return new Status().setStatus(true).setMsg("修改失败~");
    }

    /**
     * 员工是否存在
     * @param employeeDto
     * @return
     */
    @Override
    public Status isRepeatResult(EmployeeDto employeeDto) {
        if (isRepeat(employeeDto)){
            return new Status().setStatus(true).setMsg("员工已存在~");
        }
        return new Status().setStatus(false).setMsg("不存在该员工~");
    }
}
